package com.kt.app.ui.components


import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kt.app.R
import kotlinx.coroutines.launch
import kotlin.random.Random

data class Article(
    val avatar:Painter,
    val author: String,
    val title: String,
    val content: String,
    var category: String
)


@Composable
fun LazyColumnSample(articles: List<Article>) {

    val ctx = LocalContext.current

    val lazystate  = rememberLazyListState()
    val coroutineScope = rememberCoroutineScope()

    LazyColumn(state = lazystate) {
        items(articles.size) { index ->
            NewsItem(article = articles[index],morefun = {
                Toast.makeText(ctx,it.category, Toast.LENGTH_SHORT).show()
            }, modifier = Modifier.clickable {
                coroutineScope.launch {
                    lazystate.animateScrollToItem(articles.size-1)
                }

//                Toast.makeText(ctx,lazystate.firstVisibleItemIndex, Toast.LENGTH_SHORT).show()
            })
        }
    }


}

@Composable
fun NewsItem(article: Article,modifier: Modifier, morefun:(Article)->Unit) {

    ListItem(
        modifier = modifier,
        leadingContent = {
            Column(
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    contentScale = ContentScale.FillWidth,
                    modifier = Modifier.size(35.dp).clip(CircleShape),
                    painter = article.avatar,
                    contentDescription = "Home",
                )
                Text(article.author)
            }
        },
        overlineContent = { Text(text=article.category, color = Color.Blue) },
        headlineContent = { Text(text =article.title, maxLines = 1, overflow = TextOverflow.Ellipsis) },
        supportingContent = {
            Text(
                text=article.content,
                maxLines = 2,
                fontSize = 12.sp,
                overflow = TextOverflow.Ellipsis

            ) },
        trailingContent = {
            Icon(
                imageVector = Icons.Default.MoreVert,
                contentDescription = "More",
                modifier = Modifier.clickable {
                    morefun(article)
                }
        ) }
    )
}

@Preview
@Composable
fun LazyColumnSamplePreview() {
//    val artists = listOf(
//        Article(
//            avatar = painterResource(id = R.drawable.beautiful1),
//            author = "张三",
//            title = "智能眼镜销量同比增长超8倍",
//            content = "受益于AI大模型与增强现实技术的深度融合，智能眼镜市场不断升温。",
//            category = "科技快讯"
//        ),
//        Article(
//            avatar = painterResource(id = R.drawable.beautiful1),
//            author = "李四",
//            title = "AI绘画工具掀起创作革命",
//            content = "最近，多款AI绘画工具在艺术创作者中广泛使用，提升效率的同时也引发新的讨论。",
//            category = "人工智能"
//        ),
//        Article(
//            avatar = painterResource(id = R.drawable.beautiful1),
//            author = "王五",
//            title = "全球新能源汽车市场持续增长",
//            content = "2025年第一季度数据显示，全球新能源汽车销量同比增长超过40%。",
//            category = "汽车科技"
//        )
//    )

    LazyColumnSample(generateRandomArticles())
}


// 模拟作者、分类和标题/内容池
val AUTHORS = listOf("张三", "李四", "王五", "赵六", "小明", "小红")
val CATEGORIES = listOf("科技快讯", "人工智能", "汽车科技", "财经新闻", "国际要闻")
val TITLES = listOf(
    "AI大模型推动智能穿戴设备爆发增长",
    "新能源汽车行业迎来新一波融资热潮",
    "全球芯片短缺问题逐步缓解",
    "AR眼镜成为消费电子展焦点",
    "量子计算技术取得突破性进展",
    "元宇宙社交平台用户数激增",
    "自动驾驶L4级别试点扩大",
    "区块链技术应用向多领域扩展",
    "智能家居市场持续升温",
    "可穿戴设备出货量创历史新高"
)

val CONTENT_TEMPLATES = listOf(
    "受益于AI大模型与增强现实技术深度融合，智能穿戴设备市场不断升温。",
    "随着政策支持和技术进步，新能源汽车行业迎来新的发展机遇。",
    "全球芯片供应紧张局势正在逐步缓解，产业链趋于稳定。",
    "AR眼镜在消费电子展上备受关注，多家厂商发布新款产品。",
    "科研团队在量子计算领域取得重要进展，算力大幅提升。",
    "元宇宙社交平台注册用户数量在最近三个月增长超300%。",
    "多个城市启动自动驾驶L4级试点运营，覆盖物流与出行场景。",
    "区块链技术正逐步应用于金融、政务、医疗等多个行业。",
    "智能家居市场需求旺盛，2025年市场规模预计突破千亿。",
    "可穿戴设备出货量同比增长显著，健康监测功能成主流。"
)


@Composable
fun generateRandomArticles(count: Int = 50): List<Article> {
    val random = Random.Default
    return List(count) {
        val idx = random.nextInt(TITLES.size)
        Article(
            avatar = painterResource(id = R.drawable.beautiful1),
            author = AUTHORS.random(),
            title = TITLES[idx],
            content = CONTENT_TEMPLATES[idx],
            category = CATEGORIES.random()
        )
    }
}
